package com.dq.service.org.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.ServiceProjectCategoryQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.baseVo.VO;
import com.dq.domain.service.ServiceProject;
import com.dq.domain.service.ServiceProjectCategory;
import com.dq.domain.service.centre.ServiceProjectCategoryCentre;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.repository.service.ServiceProjectCategoryCentreRepository;
import com.dq.service.org.ServiceProjectCategoryService;
import com.dq.service.sys.user.SysUserService;
import com.dq.utils.TreeBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 *
 * 岗位逻辑
 */
@Service
public class ServiceProjectCategoryServiceImpl extends BaseRepositories implements ServiceProjectCategoryService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ServiceProjectCategoryCentreRepository serviceProjectCategoryCentreRepository;

    /**
     * 添加
     * @param serviceProjectCategory
     * @return
     * @throws AdminException
     */
    @Override
    public ServiceProjectCategory save(ServiceProjectCategory serviceProjectCategory)  throws AdminException {
        serviceProjectCategory.setVersion(Constant.VERSION_VALUE);
        serviceProjectCategory.setDeleted(false);
        serviceProjectCategory.setCreateTime(System.currentTimeMillis());
        serviceProjectCategory.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        serviceProjectCategory.setCreateUser(user);
        serviceProjectCategory.setUpdateUser(user);

        if(serviceProjectCategory.getSortNo() == null) {
            serviceProjectCategory.setSortNo(1);
        }

        ServiceProjectCategory flush = serviceProjectCategoryRepository.saveAndFlush(serviceProjectCategory);
        return flush;
    }

    /**
     *修改
     * @param serviceProjectCategory
     * @return
     * @throws AdminException
     */
    @Override
    public ServiceProjectCategory update(ServiceProjectCategory serviceProjectCategory) throws AdminException {
        serviceProjectCategory.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        if(user != null) {
            serviceProjectCategory.setUpdateUser(user);
        }

        ServiceProjectCategory flush = serviceProjectCategoryRepository.saveAndFlush(serviceProjectCategory);
        return flush;
    }

    /**
     * 获取所有 项目类别列表， 或根据 服务项目 获取 对应所有类别
     * @param serviceProjectCategoryQuery
     * @return
     * @throws AdminException
     */
    @Override
    public List<ServiceProjectCategory.Vo> listByPage(ServiceProjectCategoryQuery serviceProjectCategoryQuery) throws AdminException {
        serviceProjectCategoryQuery.format();
        Map<String, Object> query = serviceProjectCategoryQuery.parseMap();
        // 去除分页
        query.remove("page");
        query.remove("size");

        List<ServiceProjectCategory> orgPositionList;
        Long count;
        boolean t = false;
        if(serviceProjectCategoryQuery.getServiceProjectId() == null) {
            if (StringUtils.isNotBlank(serviceProjectCategoryQuery.getCategoryName())) {
                query.put("categoryNameLike", BaseAdminQuery.likeSql(serviceProjectCategoryQuery.getCategoryName()));
                query.remove("categoryName");
                t = true;
            }

            orgPositionList = dao.find(dao.query2Hql(ServiceProjectCategory.class, query), query);
            ///count = dao.count(dao.count2Hql(OrgPosition.class, query), query);
        }else {
            ServiceProject serviceProject = dao.get(ServiceProject.class, serviceProjectCategoryQuery.getServiceProjectId());
            if (serviceProject == null) {
                throw new AdminException("无对应 服务项目");
            }

            if (StringUtils.isNotBlank(serviceProjectCategoryQuery.getCategoryName())) {
                query.put("serviceProjectCategory_categoryNameLike", BaseAdminQuery.likeSql(serviceProjectCategoryQuery.getCategoryName()));
                query.remove("categoryName");
                t = true;
            }

            List<ServiceProjectCategoryCentre> list = dao.find(dao.query2Hql(ServiceProjectCategoryCentre.class, query), query);
            ArrayList<ServiceProjectCategory> arrayList = new ArrayList<>();
            list.forEach(node -> {
                arrayList.add(node.getServiceProjectCategory());
            });
            orgPositionList = arrayList;
            ///count = dao.count(dao.count2Hql(OrgDeptPositionCentre.class, query), query);
        }

        if(t) {
            List<ServiceProjectCategory> sons = serviceProjectCategoryRepository.findByParentInAndDeletedFalse(orgPositionList);
            orgPositionList.addAll(sons);
        }
        // tree树
        List<ServiceProjectCategory.Vo> vos = TreeBuilder.serviceProjectCategoryBuildTree(new HashSet<>((List<ServiceProjectCategory.Vo>) VO.toVOCollection(orgPositionList)));
        vos.forEach( vo -> {
            List<ServiceProject.OrgServiceProjectList> projects = new ArrayList<>();
            List<ServiceProjectCategoryCentre> list = serviceProjectCategoryCentreRepository.findByServiceProjectCategory(dao.get(ServiceProjectCategory.class,vo.getId()));
            list.forEach(serviceProjectCategoryCentre -> {
                projects.add(serviceProjectCategoryCentre.getServiceProject().toOrgServiceProjectList());
            });
            vo.setProjects(projects);
        });

        return vos;
    }

}

